﻿namespace FBlog.IntegrationTests.Database

open System
open Xunit

open FBlog.Core

open FBlog.UnitTests.Common
open FBlog.UnitTests.Common.BlogTester

[<AutoOpen>]
module RavenDbTools =

    let createRandomStoreName() =
        let baseName = "IntegrationTestData"
        let id = System.Guid.NewGuid().ToString()
        sprintf @"c:\temp\%s_%s" baseName id

    let useRavenSession(store : Raven.Client.IDocumentStore, saveChanges : bool) using  =
        use session = store.OpenSession()
        let result = using session
        if saveChanges then session.SaveChanges()
        result

    let initializeDocumentStore() =
        let storeName = createRandomStoreName()
        let documentStore = new Raven.Client.Embedded.EmbeddableDocumentStore()
        documentStore.DataDirectory <- storeName
        let store = documentStore.Initialize()
        store

module TesteBlogUndBlogPostRepositoryIntegration =

    let testEnvironment = new EnvironmentMock(testAuthor, testPublishTime)
    let testBlogPostRep = (new FBlog.Core.Repositories.BlogPostRepository(initializeDocumentStore())) :> IBlogPostRepository

    [<Fact>]
    let publishWillInsertThePostIntoTheBlogPostRepository() =
        let blog = createTestBlog(testEnvironment, testBlogPostRep)
        let blogId = publishTestPos(blog).Id

        let post = loadPostFromRepository(testBlogPostRep, blogId)
        assertPostData(post, testPublishTime)

